package com.example.black.myapplication.myhandler;

public class MyHandler {
    private MyLooper myLooper;
    private MyMessageQueue myMessageQueue;
    public MyHandler() {
        myLooper = MyLooper.getMyLooper();
        myMessageQueue = myLooper.myMessageQueue;
    }
    public void sendMessage(MyMessage myMessage){
        MyMessageQueue queue = myMessageQueue;
        if ( queue == null){
            throw new NullPointerException();
        }
        enqueueMessage(myMessage,queue);
    }

    private void enqueueMessage(MyMessage myMessage,MyMessageQueue queue) {
        myMessage.target = this;
      //  System.out.println("dispatchMessage");
        queue.enqueueMessage(myMessage);
    }

    public void dispatchMessage(MyMessage myMessage) {
       // System.out.println("dispatchMessage");
        if (myMessage.callBack != null){
             handleCallBack(myMessage.callBack);
        }else {
            handleMessage(myMessage);
        }
    }

    public void handleCallBack(Runnable callBack) {

    }

    /**
     *  子类需要集成修改
     * @param myMessage message
     */
    protected void handleMessage(MyMessage myMessage) {

    }
}
